package com.wangzemin.leetcode.num92;


public class PrintNumber {


    static volatile int prePrintNum = -1;


    public static void main(String[] args) throws InterruptedException {

        Thread a = new Thread(() -> {
            try {
                printJiShu();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread b = new Thread(() -> {
            try {
                printOuShu();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        a.start();
        b.start();
        Thread.sleep(1000);

//        while (prePrintNum < 100) {
//            synchronized (PrintNumber.class) {
//                PrintNumber.class.wait();
//            }
//
//        }
    }

    public static synchronized void printJiShu() throws InterruptedException {
        for (int i = 1; i < 100;i=i + 2) {
            if (prePrintNum == -1 || prePrintNum % 2 != 0) {
                PrintNumber.class.notifyAll();
                PrintNumber.class.wait();
            }
            System.out.print(i + ",");
            prePrintNum = i;

        }
    }

    public static synchronized void printOuShu() throws InterruptedException {
        for (int i = 0; i <= 100; i=i + 2) {
            if (prePrintNum % 2 == 0) {
                PrintNumber.class.notifyAll();
                PrintNumber.class.wait();
            }
            System.out.print(i + ",");
            prePrintNum = i;

        }
    }
}

